﻿using System;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class MultilineTextFieldType : SimpleFieldType<MultilineTextFieldDto>
    {
        public MultilineTextFieldType(Guid fieldId) : base(fieldId)
        {
        }

        public override MultilineTextFieldDto GetValue(SPListItem item)
        {
            var multiLineTextField = GetField<SPFieldMultiLineText>(item);
            object fieldValue = item[FieldId];
            if (fieldValue == null)
            {
                return null;
            }
            string text = multiLineTextField.GetFieldValueAsText(fieldValue);
            string html = multiLineTextField.GetFieldValueAsHtml(fieldValue);
            return new MultilineTextFieldDto(text, html);
        }

        public override void SetValue(SPListItem item, MultilineTextFieldDto value)
        {
            if (value != null)
            {
                item[FieldId] = value.Text;
            }
            else
            {
                item[FieldId] = string.Empty;
            }
        }
    }
}